package com.rerun.controller.college;

import java.util.List;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.rerun.common.anno.Log;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.College;
import com.rerun.entity.SysRole;
import com.rerun.entity.SysUser;
import com.rerun.entity.SysUserRole;
import com.rerun.service.ICollegeService;
import com.rerun.service.IMemberUserService;
import com.rerun.service.ISysDeptService;
import com.rerun.service.ISysRoleService;
import com.rerun.service.ISysUserRoleService;
import com.rerun.service.ISysUserService;
import com.rerun.vo.memberUser.StudentVo;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.swing.text.html.parser.Entity;


/**
 * The type College controller.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /9/25 16:52
 */
@Controller
@RequestMapping("/college")
public class CollegeController extends SuperController {
    @Resource
    private ICollegeService iCollegeService;
    @Autowired
	private IMemberUserService memberUserService;
    @Autowired 
    private ISysRoleService sysRoleService;
	@Autowired 
	private ISysDeptService sysDeptService;
	@Autowired 
	private ISysUserService sysUserService;
	@Autowired 
	private ISysUserRoleService sysUserRoleService;

    /**
     * 分页查询 师范院校.
     *
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @param search     the search
     * @param model      the model
     * @return the string
     */
    @RequiresPermissions("listCollege")
    @RequestMapping("/list/{pageNumber}")
    public String list(@PathVariable("pageNumber") Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize, String search, Model model) {
        Page<College> collegePage = getPage(pageNumber, pageSize);
        model.addAttribute("pageSize", pageSize);
        EntityWrapper<College> collegeEntityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            collegeEntityWrapper.like("collegeName", search);
            model.addAttribute("search", search);
        }
        Page<College> data = iCollegeService.selectPage(collegePage, collegeEntityWrapper);
        model.addAttribute("pageData", data);
        return "business/college/list";
    }

    /**
     * 新增院校页面.
     *
     * @return the string
     */
    @RequiresPermissions("addCollege")
    @RequestMapping("/add")
    public String add() {
        return "business/college/add";
    }

    /**
     * 添加院校.
     *
     * @param college the college
     * @return the rest
     */
    @RequiresPermissions("addCollege")
    @Log("增加院校")
    @RequestMapping("/addCollege")
    @ResponseBody
    public Rest addCollege(College college) {
        iCollegeService.insert(college);
        return Rest.ok();
    }

    /**
     * 编辑院校页面.
     *
     * @param collegeId the college id
     * @param model     the model
     * @return the string
     */
    @RequiresPermissions("editCollege")
    @RequestMapping("/edit/{collegeId}")
    public String edit(@PathVariable(value="collegeId") String collegeId,Model model){
      College college = iCollegeService.selectById(collegeId);
    
        model.addAttribute("college",college);
        return "business/college/editCollege";
    }

    /**
     * 编辑院校.
     *
     * @param college the college
     * @return the rest
     */
    @RequiresPermissions("editCollege")
    @Log("编辑院校")
    @RequestMapping("/editCollege")
    @ResponseBody
    public Rest editCollege(College college){
    	
        iCollegeService.updateById(college);
        return Rest.ok();
    }

    /**
     * 导入数据页面.
     *
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @param search     the search
     * @param model      the model
     * @return the string
     */
    @RequestMapping("/importPage/{pageNumber}")
    public String importPage(@PathVariable("pageNumber") Integer pageNumber,
			@RequestParam(defaultValue = "15") Integer pageSize, String search, Model model) {
    	Page<StudentVo> studentPage = getPage(pageNumber, pageSize);
		model.addAttribute("pageSize", pageSize);
		
		//获取当前登录用户
    	Subject subject = SecurityUtils.getSubject();
		SysUser sysUser = (SysUser) subject.getPrincipal();
		
		StudentVo studentVo = new StudentVo();
		String collegeId = sysUser.getCompanyId();
		if(StringUtils.isNotEmpty(collegeId)) {
			studentVo.setCollegeId(collegeId);
		}
		if(StringUtils.isNoneEmpty(search)) {
			studentVo.setPhone(search);
			studentVo.setName(search);
		}
		Page<StudentVo> data = memberUserService.selectStudentPage(studentPage, studentVo);
		model.addAttribute("pageData", data);
		model.addAttribute("search", search);
		//状态
        return "/business/college/importData";
    }
   
    @Log("查看管理员")
    @RequestMapping("/user/list/{collegeid}")  
    public  String userList(Model model ,@PathVariable (value="collegeid") String collegeid){
    	EntityWrapper<SysUser> wrapper = new EntityWrapper<SysUser>();
    	wrapper.where("companyId={0}", collegeid);
    	List<SysUser> list=sysUserService.selectList(wrapper);
    	model.addAttribute("list",list);
		return "/business/college/managerList";
    }
    /**
     * 添加管理员
     *
     * @param model the model
     * @return the string
     */
    @RequiresPermissions("addUser")
    @RequestMapping("/addManager/{collegeid}")  
    public  String addManager(Model model, @PathVariable (value="collegeid") String collegeid){
    	model.addAttribute("roleList", sysRoleService.selectList(null));
    	model.addAttribute("deptList", sysDeptService.selectList(null));
		return "/business/college/addmanager";
    }

    /**
     * 执行新增
     *
     * @param user   the user
     * @param roleId the role id
     * @return the rest
     */
    @Log("创建用户")
    @RequiresPermissions("addUser")
    @RequestMapping("/doAdd")  
    @ResponseBody
    public Rest doAdd(SysUser user, @RequestParam(value="roleId[]",required=false) String[] roleId){
    	
    	sysUserService.insertUser(user,roleId);
    	return Rest.ok();
    }

}